/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   ConstructionEffectServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.construction    </p>
 * 
 * <p>最后修改 : $: Sep 5, 2017-2:19:11 PM v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.yh.construction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.BuildingEffectsDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.BuildingEffects;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.construction.ConstructionEffectService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.StringUtils;

/**
 * 
 * ConstructionEffectServiceImpl
 * 
 * Sep 5, 2017 2:19:11 PM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class ConstructionEffectServiceImpl implements ConstructionEffectService{
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private BuildingEffectsDao buildingEffectsDao;
	
	@Override
	public List<BuildingEffects> getBuildingEffectsList(QueryFilter queryFilter){
		return buildingEffectsDao.find(queryFilter, "checkYear", true);
	}
	
	@Override
	public Map<String, Object> getBuildingEffectsMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<BuildingEffects> buildingEffectsList = new ArrayList<BuildingEffects>();
		List<Object> values = new ArrayList<Object>();
 		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
 		//两条道路都形成全线规模效应
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//有1条道路效果明显或有两项不足或示范效明显不足
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//2条道路都存在明显不足得分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		
		String roadId = (String)jsonObject.get("roadName");
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachmentsByParTag(tag);
		
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull;	//满分
		
		boolean flag = false;
		int sumUnSatisfyNum = 0;
		int sumEffict = 0;
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isEmpty(roadId)) {
			buildingEffectsList = buildingEffectsDao.getAll();
		} else {
			values.add(roadId);
			flag = true;
			queryFilter.addFilter("id", roadId, MatchType.EQ);
			buildingEffectsList = this.getBuildingEffectsList(queryFilter);
			for (BuildingEffects buildingEffects : buildingEffectsList) {
				sumUnSatisfyNum += buildingEffects.getUnSatisfyNum() != null? buildingEffects.getUnSatisfyNum(): 0;
				
				if ("1".equals(buildingEffects.getDemonstrationEffect())) {
					sumEffict += 1;
				} else if ("3".equals(buildingEffects.getDemonstrationEffect())) {
					sumEffict += 0;
				}
			}
		}
		
		if (sumEffict >= 2 ) {
			total += yearFull;
		} else if (sumEffict == 1 ||  sumUnSatisfyNum > 2) {
			total += yearPart;
		} else if (sumEffict <1 ) {
			sumEffict += poitMark;
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("percent", percentage);
		map.put("flag", flag);
		map.put("buildingEffectsList", buildingEffectsList);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
	
	
}
